<?php
include('koneksi.php');

function berita_hot(){
	$q = "SELECT id_berita, judul, detail, gambar, DATE_FORMAT(waktu_post,'%e %M %Y %h:%i %p') as waktu,(SELECT count(id_berita) as jumlah FROM komentar) as komentar FROM berita WHERE status=1 ORDER BY waktu desc limit 1";	
	$hasil= mysql_query($q) or exit("Error query : <b>".$q."</b>.");
	$data= mysql_fetch_assoc($hasil);
	echo '<h1><a href="detail.php?id='.$data['id_berita'].'">'.$data['judul'].'</a></h1><p class="author">KitaPost | <span>'.$data['waktu'].'</span> | <span>'.$data['komentar'].' komentar</span></p><img src="gambar/'.$data['gambar'].'" alt="" width="400px" height="auto" />
        <p>'.substr($data['detail'],0,150).'</p>
        <p><a href="detail.php?id='.$data['id_berita'].'">Baca selengkapnya &raquo;</a></p>';
}

function kategori(){
	$q = "SELECT id_kategori, kategori FROM kategori limit 9";	
	$hasil= mysql_query($q) or exit("Error query : <b>".$q."</b>.");
	while($isi = mysql_fetch_assoc($hasil)){
		$data[] = $isi['kategori'];
	}
	return $data;
}
function feature(){
	$q = "SELECT id_berita, judul, detail, DATE_FORMAT(waktu_post,'%e %M %Y %h:%i %p') as waktu FROM berita WHERE status=2 ORDER BY id_berita DESC limit 4";	
	$hasil= mysql_query($q) or exit("Error query : <b>".$q."</b>.");
	while($isi = mysql_fetch_assoc($hasil)){
		echo '<h2><a href="detail.php?id='.$isi['id_berita'].'">'.$isi['judul'].'</a></h2>
        <p class="author">KitaPost | <span>'.$isi['waktu'].'</span></p>
        <p>'.substr($isi['detail'],0,100).'</p>
        <p><a href="detail.php?id='.$isi['id_berita'].'">Baca selengkapnya &raquo;</a></p>';
	}
}
function olahraga(){
	$q = "select b.id_berita, b.judul, b.gambar, b.waktu_post, k.kategori from berita b
	inner join kategori k on k.id_kategori=b.id_kategori
	where b.id_kategori = 5 and status = 3 order by b.waktu_post desc limit 1";	
	$hasil= mysql_query($q) or exit("Error query : <b>".$q."</b>.");
	while($isi = mysql_fetch_assoc($hasil)){
		echo '<img src="gambar/'.$isi['gambar'].'" alt="'.$isi['judul'].'" width="280px"/>
				<h3><a href="detail.php?id='.$isi['id_berita'].'">'.$isi['judul'].'</a></h3>
				<p><a href="kategori.php?id='.$isi['kategori'].'">Indeks '.$isi['kategori'].' &raquo;</a></p>';
	}
}
function dunia(){
	$q = "select b.id_berita, b.judul, b.gambar, b.waktu_post, k.kategori from berita b
inner join kategori k on k.id_kategori=b.id_kategori
where b.id_kategori = 2 and status = 3 order by b.waktu_post desc limit 1";	
	$hasil= mysql_query($q) or exit("Error query : <b>".$q."</b>.");
	while($isi = mysql_fetch_assoc($hasil)){
		echo '<img src="gambar/'.$isi['gambar'].'" alt="'.$isi['judul'].'" width="280px"/>
				<h3><a href="detail.php?id='.$isi['id_berita'].'">'.$isi['judul'].'</a></h3>
				<p><a href="kategori.php?id='.$isi['kategori'].'">Indeks '.$isi['kategori'].' &raquo;</a></p>';
	}
}
function politik(){
	$q = "select b.id_berita, b.judul, b.gambar, b.waktu_post, k.kategori from berita b
inner join kategori k on k.id_kategori=b.id_kategori
where b.id_kategori = 3 and status = 3 order by b.waktu_post desc limit 1";	
	$hasil= mysql_query($q) or exit("Error query : <b>".$q."</b>.");
	while($isi = mysql_fetch_assoc($hasil)){
		echo '<img src="gambar/'.$isi['gambar'].'" alt="'.$isi['judul'].'" width="280px"/>
				<h3><a href="detail.php?id='.$isi['id_berita'].'">'.$isi['judul'].'</a></h3>
				<p><a href="kategori.php?id='.$isi['kategori'].'">Indeks '.$isi['kategori'].' &raquo;</a></p>';
	}
}
function kesehatan(){
	$q = "select b.id_berita, b.judul, b.gambar, b.waktu_post, k.kategori from berita b
inner join kategori k on k.id_kategori=b.id_kategori
where b.id_kategori = 4 and status = 3 order by b.waktu_post desc limit 1";	
	$hasil= mysql_query($q) or exit("Error query : <b>".$q."</b>.");
	while($isi = mysql_fetch_assoc($hasil)){
		echo '<img src="gambar/'.$isi['gambar'].'" alt="'.$isi['judul'].'" width="280px"/>
				<h3><a href="detail.php?id='.$isi['id_berita'].'">'.$isi['judul'].'</a></h3>
				<p><a href="kategori.php?id='.$isi['kategori'].'">Indeks '.$isi['kategori'].' &raquo;</a></p>';
	}
}
function tekno(){
	$q = "select b.id_berita, b.judul, b.gambar, b.waktu_post, k.kategori from berita b
inner join kategori k on k.id_kategori=b.id_kategori
where b.id_kategori = 6 and status = 3 order by b.waktu_post desc limit 1";	
	$hasil= mysql_query($q) or exit("Error query : <b>".$q."</b>.");
	while($isi = mysql_fetch_assoc($hasil)){
		echo '<img src="gambar/'.$isi['gambar'].'" alt="'.$isi['judul'].'" width="280px"/>
				<h3><a href="detail.php?id='.$isi['id_berita'].'">'.$isi['judul'].'</a></h3>
				<p><a href="kategori.php?id='.$isi['kategori'].'">Indeks '.$isi['kategori'].' &raquo;</a></p>';
	}
}
function info_seleb(){
	$q = "select id_berita, judul, gambar, detail, waktu_post from berita where id_kategori = 7 and status = 3 order by waktu_post desc limit 1";	
	$hasil= mysql_query($q) or exit("Error query : <b>".$q."</b>.");
	$data= mysql_fetch_assoc($hasil);
	echo '<img src="gambar/'.$data['gambar'].'" alt="" width="400px" height="auto" />
		<h1><a href="detail.php?id='.$data['id_berita'].'">'.$data['judul'].'</a></h1>
        <p class="author">KitaPost | <span>09.18.09</span></p>
        <p>'.substr($data['detail'],0,150).'</p>
        <p><a href="#">Index Info Seleb &raquo;</a></p>';
}
function fashion(){
	$q = "select id_berita, judul, gambar, waktu_post from berita where id_kategori = 9 and status = 3 order by waktu_post desc limit 3";	
	$hasil= mysql_query($q) or exit("Error query : <b>".$q."</b>.");
	while($isi = mysql_fetch_assoc($hasil)){
		echo '<img src="gambar/'.$isi['gambar'].'" alt="" width="150px"/>
		<p><a href="detail.php?id='.$isi['id_berita'].'" alt="'.$isi['judul'].'">'.$isi['judul'].'</a></p>';
	}
}
function berita_terkait($id_kategori){
	$q = "select id_berita, judul, detail, DATE_FORMAT(waktu_post,'%e %M %Y %h:%i %p') as waktu from berita where id_kategori = ".$id_kategori." order by waktu desc limit 4";	
	$hasil= mysql_query($q) or exit("Error query : <b>".$q."</b>.");
	while($isi = mysql_fetch_assoc($hasil)){
		echo '<li><a href="detail.php?id='.$isi['id_berita'].'"><b>'.$isi['judul'].'</b></a>&nbsp;|&nbsp;<i>'.$isi['waktu'].'</i><br/><p>'.substr($isi['detail'],0,150).'...</p>
		</li>';
	}
}
function baca($id){
	$upd = "UPDATE berita SET counter=counter+1 WHERE id_berita=".$id;
	mysql_query($upd) or exit("Error query : <b>".$upd."</b>.");
	$q = "SELECT id_berita, judul, detail, id_kategori, gambar, 	DATE_FORMAT(waktu_post,'%e %M %Y %h:%i %p') as waktu, counter 
		FROM berita 
		WHERE id_berita=".$id;
	$hasil= mysql_query($q) or exit("Error query : <b>".$q."</b>.");
	$data= mysql_fetch_assoc($hasil);
	return $data;
}
function terbanyak(){
	$q = "select id_berita, judul from berita order by counter desc limit 5";
	$hasil= mysql_query($q) or exit("Error query : <b>".$q."</b>.");
	return $hasil;
}
function terunik(){
	$q = "select id_berita, judul from berita where id_kategori=10 order by counter desc limit 5";
	$hasil= mysql_query($q) or exit("Error query : <b>".$q."</b>.");
	return $hasil;
}
function film(){
	$q = "select id_berita, judul from berita where id_kategori=11 order by counter desc limit 5";
	$hasil= mysql_query($q) or exit("Error query : <b>".$q."</b>.");
	return $hasil;
}
function kategori_tampil($id){
	$q = "SELECT id_kategori, kategori FROM kategori WHERE kategori='".$id."'";
	$hasil= mysql_query($q) or exit("Error query : <b>".$q."</b>.");
	$data= mysql_fetch_assoc($hasil);
	return $data;
}

function kategori_show($id_kategori){
	$q = "select id_berita, judul, detail, gambar, DATE_FORMAT(waktu_post,'%e %M %Y %h:%i %p') as waktu from berita where id_kategori = ".$id_kategori." order by waktu desc limit 4";
	$hasil= mysql_query($q) or exit("Error query : <b>".$q."</b>.");
	while($isi = mysql_fetch_assoc($hasil)){
		echo '<div class="kotak-kategori">
			<h2><a href="detail.php?id='.$isi['id_berita'].'">'.$isi['judul'].'</a></h2>
			<img class="gbr-oke" alt="" src="gambar/'.$isi['gambar'].'">
			<p>'.substr($isi['detail'],0,100).'</p>
		</div>';
	}
}
function komentar($id_berita, $komentar, $nama, $email){
	$q = "INSERT INTO komentar (id_berita, komentar, nama, email, waktu_input) VALUES (".$id_berita.", '".$komentar."', '".$nama."', '".$email."', '".date('Y-m-d H:i:s')."');";	
	$hasil= mysql_query($q) or exit("Error query : <b>".$q."</b>.");
	
	echo "<script>
			window.location = '../pwl/detail.php?id=".$id_berita."';
	</script>";
}
function tampil_komentar($id){
	$q = "SELECT nama, komentar, email, DATE_FORMAT(waktu_input,'%e %M %Y') as waktu FROM komentar WHERE id_berita=".$id;
	$hasil= mysql_query($q) or exit("Error query : <b>".$q."</b>.");
	while($isi = mysql_fetch_assoc($hasil)){
		echo '<small><a href="mailto:'.$isi['email'].'"><b>'.$isi['nama'].' </b></a>|<i> Pada '.$isi['waktu'].'</i></small><br/><br/>
			<p><i>"'.$isi['komentar'].'"</i></p>
			<hr/>';
	}
}
function hitung_komentar($id){
	$q = "SELECT count(id_berita) as jumlah FROM komentar WHERE id_berita=".$id;
	$hasil= mysql_query($q) or exit("Error query : <b>".$q."</b>.");
	while($isi = mysql_fetch_assoc($hasil)){
		echo '<i>'.$isi['jumlah'].' komentar</i>';
	}
}
function cari($id){
	$q = "select id_berita, judul, detail, gambar, DATE_FORMAT(waktu_post,'%e %M %Y %h:%i %p') as waktu 
		from berita 
		where judul like '%".$id."%'
		order by waktu desc limit 10";
	$hasil= mysql_query($q) or exit("Error query : <b>".$q."</b>.");
	if($cek = mysql_num_rows($hasil)){
	while($isi = mysql_fetch_assoc($hasil)){		
		echo '<div class="kotak-kategori">
					<h2><a href="detail.php?id='.$isi['id_berita'].'">'.$isi['judul'].'</a></h2>
					<img class="gbr-oke" alt="" src="gambar/'.$isi['gambar'].'">
					<p>'.substr($isi['detail'],0,100).'</p>
				</div><br/>';
	}
	}else{
		echo '<div class="kotak-kategori">
					<h4><i>Berita tentang "'.$id.'" tidak ditemukan</i></h4>
				</div><br/>';
	}
}
?>